package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.config.vip.VipInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.utils.http.HttpHelper;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/usercenter/dict")
public class CenterCommonDictController {

    //区服列表
    @ResponseBody
    @RequestMapping(value = "serverInfoList", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String serverInfoList(){
        Map<Object, Object> objectMap = new HashMap<>();
        for (ServerInfo info : ServerListMgr.getServerList()) {
            objectMap.put(info.getServerId(), info.getServerName());
        }
        return JSON.toJSONString(objectMap);
    }

    //道具
    @ResponseBody
    @RequestMapping(value = "goodsInfoList", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String goodsInfoList(@RequestParam("channelId") int channelId){
        List<ServerInfo> channelServerList = ServerListMgr.getChannelServerList(channelId);
        Map<Object, Object> objectMap = new HashMap<>();
        if(channelServerList.isEmpty()){
            return JSON.toJSONString(objectMap);
        }
        int idx = new Random().nextInt(channelServerList.size());
        ServerInfo serverInfo = channelServerList.get(idx);
        if(Config.isDebug()){
            serverInfo = ServerListMgr.getServerInfo(99938028L);
        }
        // 转发消息到对应的服务器上
        String result = HttpHelper.doPostJson(serverInfo.getWebServerUrl() + "/queryGoodsNameList", "", "UTF-8");
        return result;
    }


    //vip
    @ResponseBody
    @RequestMapping(value = "vipInfoList", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String vipInfoList(){
        Map<Object, Object> objectMap = new HashMap<>();
        for (Map.Entry<Integer, VipInfo> entry : VipMgr.getVipInfoMap().entrySet()) {
            objectMap.put(entry.getValue().getLevel(), entry.getValue().getVipExp());
        }
        return JSON.toJSONString(objectMap);
    }


    //途径
    @ResponseBody
    @RequestMapping(value = "wayInfoList", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String wayInfoList(){
        Map<Object, Object> objectMap = new HashMap<>();
        for (eLogMoneyType type : eLogMoneyType.values()){
            objectMap.put(type.getValue(), type.getName());
        }
        return JSON.toJSONString(objectMap);
    }

}
